#include "MessageSubscriber.h"
#include <fastrtps/attributes/ParticipantAttributes.h>
#include <fastrtps/attributes/SubscriberAttributes.h>
#include <fastdds/dds/domain/DomainParticipantFactory.hpp>
#include <fastdds/dds/subscriber/Subscriber.hpp>
#include <fastdds/dds/subscriber/DataReader.hpp>
#include <fastdds/dds/subscriber/SampleInfo.hpp>
#include <fastdds/dds/subscriber/qos/DataReaderQos.hpp>

using namespace eprosima::fastdds::dds;
using namespace eprosima::fastrtps::rtps;
bool MessageSubscriber_ds::init(InitParam param)
{
    listener_->zeroCopy_ = param.zeroCopy;
    DomainParticipantQos pqos = PARTICIPANT_QOS_DEFAULT;
    pqos.wire_protocol().builtin.discovery_config.ignoreParticipantFlags = ParticipantFilteringFlags::FILTER_SAME_PROCESS;

    pqos.name("DomainParticipant_pub_data_sharing");
    participant_ = DomainParticipantFactory::get_instance()->create_participant(param.domain_id, pqos);
    if (!participant_) {
        return false;
    }
    type_.register_type(participant_);
    subscriber_ = participant_->create_subscriber(SUBSCRIBER_QOS_DEFAULT);
    if (!subscriber_) {
        return false;
    }

    topic_ = participant_->create_topic(param.topic, "Message", TOPIC_QOS_DEFAULT);
    if (!topic_) {
        return false;
    }
    DataReaderQos rqos = DATAREADER_QOS_DEFAULT;
    rqos.history().kind = KEEP_LAST_HISTORY_QOS;
    rqos.history().depth = param.depth;
    rqos.resource_limits().max_samples = param.max_samples;
    rqos.resource_limits().allocated_samples = param.allocated_samples;
    rqos.reliability().kind = RELIABLE_RELIABILITY_QOS;
    rqos.durability().kind = TRANSIENT_LOCAL_DURABILITY_QOS;
    rqos.data_sharing().automatic();
    listener_ = new SubListener;
    reader_ = subscriber_->create_datareader(topic_, rqos, listener_);
    if (!reader_) {
        return false;
    }

    return true;
}